{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# Python变量和数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "## 字符串\n",
    "\n",
    "Python中对字符串的定义：\n",
    "\n",
    "> Textual data in Python is handled with `str` objects, or **strings**. Strings are immutable sequences of Unicode code points.\n",
    "\n",
    "Python中的文本数据是通过`str`对象或字符串来处理的，字符串是由一系列Unicode码位（code point）所组成的**不可变序列**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "('S' 'T' 'R' 'I' 'N' 'G')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Unicode 暂时可以看作一张非常大的地图，这张地图里面记录了世界上所有的符号，而码位则是每个符号所对应的坐标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "s = '春分'\n",
    "print(s)\n",
    "print(len(s))\n",
    "print(s.encode())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "使用内建函数`len()`可以获得字符串的长度。\n",
    "\n",
    "**不可变**是指无法对字符串本身进行更改操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "s = 'Hello'\n",
    "print(s[3])\n",
    "s[3] = 'o'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "而**序列（sequence）**则是指字符串继承序列类型（`list/tuple/range`）的通用操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "list1 = []\n",
    "for i in 'Hello':\n",
    "    list1.append(i.upper())\n",
    "\n",
    "# list2 = [i.upper() for i in 'Hello']\n",
    "# list1 == list2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "**序列**是容器类型，“成员”们站成了有序的队列，我们从0开始进行对每个成员进行标记，0, 1, 2, 3…，这样，便可以通过**下标**访问序列的一个或几个成员，就像C语言中的数组一样。接下来，我们先来了解一下序列。\n",
    "\n",
    "### 序列类型操作符\n",
    "\n",
    "**注：以下操作符对所有序列类型都适用。**\n",
    "\n",
    "#### 成员关系操作符（`in`、`not in`）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "'x' in 'china'                             # 返回 False\n",
    "'e' not in 'pity'                          # 返回 True\n",
    "12 in [13, 32, 4, 0]                       # 返回 False\n",
    "'green' not in ['red', 'yellow', 'white']  # 返回 True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "#### 连接操作符（`+`）\n",
    "\n",
    "注：只可用于同种类型序列连接。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "str1 = 'aaa'\n",
    "str2 = 'bbb'\n",
    "str2 = str1 + str2\n",
    "str2             # 返回'aaabbb'，此时str2所指向的对象是新创建的对象\n",
    "                 # 因字符串是不可更新的标量，可以用id()测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "num_list = [1, 3, 5]\n",
    "num_list += [7, 9]\n",
    "num_list         # 返回[1, 3, 5, 7, 9]，此时的num_list指向的对象还是\n",
    "                 # 原始对象，因其是可更改的容器，可以用id()测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "(1, 3) + (5, 7)  # 返回(1, 3, 5, 7)，注意元组是不可更改的容器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "#### 重复操作符（`*`）\n",
    "\n",
    "用法：`s * n` 或 `n * s`\n",
    "\n",
    "`*`用以将序列重复指定次数，如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "astr = 'hello'\n",
    "astr *= 3\n",
    "astr               # 返回'hellohellohello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "alpha_list = ['a', 'b', 'c']\n",
    "alpha_list *= 2\n",
    "alpha_list         # 返回['a', 'b', 'c', 'a', 'b', 'c']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "('ha', 'ya') * 3  # 返回('ha', 'ya', 'ha', 'ya', 'ha', 'ya')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "**当`n`的值小于0的时候都按照`n = 0`对待（结果将返回一个和`s`类型相同的空序列）。**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "'a' * -2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "**另外需要注意的是序列`s`中的元素并没有被复制，它们只是被引用了多次。**这个需要Python初学者特别注意，比如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "lists = [[]] * 3\n",
    "lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "lists[0].append(3)\n",
    "lists"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "`[[]]`是一个单元素列表，包含一个空的列表，所以`[[]] * 3`中的3个元素都引用这个空的列表。修改其中的任意一个元素都会修改这个空的列表。你可以用下面的方式创建一个不同列表构成的列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "lists = [[] for i in range(3)]\n",
    "lists[0].append(3)\n",
    "lists[1].append(5)\n",
    "lists[2].append(7)\n",
    "lists"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "更多的解释可以参考Python官方文档：[How do I create a multidimensional list?](https://docs.python.org/3.6/faq/programming.html#faq-multidimensional-list)\n",
    "\n",
    "#### 切片操作符（`[]`、`[:]`、`[::]`）\n",
    "\n",
    "通过切片功能可以访问序列的一个或者多个成员。和C一样，在访问单个成员时你要保证你访问下标的成员是存在的，否则会引发`IndexError`异常（C中叫做数组越界）。\n",
    "\n",
    "##### 索引——访问单个成员`[]`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "astr = 'Python'\n",
    "astr[0]\n",
    "astr[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "astr[6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "astr[-1]  # 'n'\n",
    "astr[-6]  # 'P'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "注意，因为`-0`等于`0`，负数的索引从`-1`开始。\n",
    "\n",
    "##### 切片——访问连续的多个成员`[starting_index : ending_index]`\n",
    "\n",
    "切片索引有默认值，默认的起始索引是`0`，默认的终止索引是所要切片的字符串的长度。\n",
    "参考[Common Sequence Operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations) Notes 4:\n",
    "> The slice of <em>s</em> from <em>i</em> to <em>j</em> is defined as the sequence of items with index <em>k</em> such that `i <= k < j`. \n",
    "> If <em>i</em> or <em>j</em> is greater than `len(s)`, use `len(s)`. If <em>i</em> is omitted or `None`, use `0`. \n",
    "> If <em>j</em> is omitted or `None`, use `len(s)`. \n",
    "> If <em>i</em> is greater than or equal to <em>j</em>, the slice is empty."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "astr[:]     # 'Python'\n",
    "astr[0:]    # 'Python'\n",
    "astr[:6]    # 'Python'\n",
    "astr[:5]    # 'Pytho'\n",
    "astr[5:]    # 'n'\n",
    "astr[:-1]   # 'Pytho'\n",
    "astr[1:-1]  # 'ytho'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "注意起始索引是包含进来的，终止索引是排除在外的。所以，**`s[:i] + s[i:]`永远等于`s`。**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "astr[:2] + astr[2:]\n",
    "astr[:4] + astr[4:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "记住切片如何工作的一种方法是将索引看作是字符间的点，第一个字符的左侧的位置为`0`，最后一个字符的右侧的位置为字符的长度。比如：\n",
    "\n",
    "```\n",
    " +---+---+---+---+---+---+\n",
    " | P | y | t | h | o | n |\n",
    " +---+---+---+---+---+---+\n",
    " 0   1   2   3   4   5   6\n",
    "-6  -5  -4  -3  -2  -1\n",
    "```\n",
    "\n",
    "另外需要注意的是，当使用切片访问连续的多个成员时超出索引范围将被很好的处理。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "##### 以等差数列形式的下标进行访问 `[starting_index : ending_index : step_length]`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "`step_length` 为正表示从左到右切片，反之为右到左，然后根据index依次切片。\n",
    "\n",
    "* `step_length`为正，则从左到右切片，如果 `starting_index > ending_index`，则为空\n",
    "* `step_length`为负，则从右到左切片，如果 `starting_index < ending_index`，则为空\n",
    "* `starting_index` 和 `ending_index` 填空，前者表示**最开始**，后者表示**最后一个**, 同时为空的时候，表示取所有。\n",
    "  至于方向，取决于 `step_length` 。\n",
    "\n",
    "\n",
    "参考[Common Sequence Operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations) Notes 5：\n",
    "\n",
    "> The slice of <em>s</em> from <em>i</em> to <em>j</em> with step <em>k</em> is defined as the sequence of items with index `x = i + n*k` \n",
    "> such that `0 <= n < (j-i)/k`. In other words, the indices are `i`, `i+k`, `i+2*k`, `i+3*k` and so on, \n",
    "> stopping when <em>j</em> is reached (but never including </em>j</em>). When <em>k</em> is positive, <em>i</em> and <em>j</em> are reduced to \n",
    "> `len(s)` if they are greater. When <em>k</em> is negative, <em>i</em> and <em>j</em> are reduced to `len(s) - 1` if they are greater. \n",
    "> If <em>i</em> or <em>j</em> are omitted or `None`, they become “end” values (which end depends on the sign of <em>k</em>). \n",
    "> Note, <em>k</em> cannot be zero. If <em>k</em> is `None`, it is treated like `1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "(1, 2, 3, 4, 5, 6)[0:6:2]  # 返回 (1, 3, 5)\n",
    "bstr = \"abcdefg\"\n",
    "bstr[::-1]    # 返回'gfedcba'，瞬间反转，未添加的参数默认为开始和结束\n",
    "bstr[::]      # 返回'abcdefg'，未添加的参数都使用默认值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "cstr = '012345'\n",
    "cstr[len(cstr)-1::-1]   # '543210'\n",
    "cstr[None::-1]   # '543210'\n",
    "cstr[-1::-1]  # '543210'\n",
    "cstr[-1:None:-1]  # '543210'\n",
    "cstr[-1:len(cstr)-1:-1]  # ''\n",
    "cstr[:0:-1]  # '54321'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "#### 用于序列的内建函数\n",
    "\n",
    "* `max()` 返回序列中的最大值\n",
    "* `min()` 返回序列中最小值\n",
    "* `sum()` 返回列表的元素之和\n",
    "* `enumerate(iter)` 接受一个可迭代对象，返回一个enumerate对象，该对象生成iter的每个成员的index值和item值构成的数组\n",
    "* `reversed(seq)` 返回一个序列的逆向迭代器\n",
    "* `sorted()` 对一个序列，排序，返回排好序的列表，可以指定排序方法\n",
    "* `zip()` 返回一个zip对象，其成员为元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "list_demo = [1, 43, 4, 54]\n",
    "max(list_demo)             # 54\n",
    "min(list_demo)             # 1\n",
    "sum(list_demo)             # 102\n",
    "list(reversed(list_demo))  # [54, 4, 43, 1]\n",
    "sorted(list_demo)          # [1, 4, 43, 54]\n",
    "list(zip(list_demo, list_demo[::-1]))  # [(1, 54), (43, 4), (4, 43), (54, 1)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "上面是对序列简单的介绍，接着我们回到字符串上来。\n",
    "\n",
    "### 字符串的创建\n",
    "\n",
    "3种方式创建字符串字面量：\n",
    "\n",
    "1. 单引号：`'allows embedded \"double\" quotes'`\n",
    "2. 双引号：`\"allows embedded 'single' quotes\"`\n",
    "3. 三引号：`'''Three single quotes'''` `\"\"\"Three double quotes\"\"\"`\n",
    "\n",
    "其中，三引号创建的字符串可以跨越多行，其中的空白（例如每行的换行符以及行首或行末的空格）会被包含进所创建的字符串字面量。\n",
    "\n",
    "Python允许空字符串`''`，它不包含任何字符但完全合法。空字符串是其他任何字符串的子串。\n",
    "\n",
    "字符串字面量是一个单独的表达式，如果多个字符串字面量中间仅包含空白，则它们将被隐性地转换为一个单一的字符串字面量。所以，`(\"spam\" \"eggs\") == \"spameggs\"`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "url_str = ('http://'       # protocol\n",
    "           'localhost'     # hostname\n",
    "           ':8080'         # port\n",
    "           '/index.html')  # file\n",
    "url_str  # 'http://localhost:8080/index.html'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "另外，你还可以使用`str`构造器将其它对象转换为字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "str(98.6)  # '98.6'\n",
    "str(True)  # 'True'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "### 使用`\\`转义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "常见的转义符：`\\n`（换行符）、`\\t`（Tab制表符）、`\\r`（回车）、`\\'`（单引号）、`\\\"`（双引号）、`\\\\`（反斜线）\n",
    "\n",
    "### 字符串操作符\n",
    "\n",
    "参考上面**序列类型操作符**，不再赘述。\n",
    "\n",
    "### 用于字符串的内建函数\n",
    "\n",
    "* `input()` 获取用户输入，返回一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "name = input(\"What's your name: \")\n",
    "print(\"Your name is %s.\" % name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "ord(chr(64))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "dir(str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "* `chr()` 接受一个整数，返回对应的Unicode字符\n",
    "* `ord()` 功能与`chr()` 相反"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "### 字符串方法\n",
    "\n",
    "用法：`string_object.method(arguments)`\n",
    "\n",
    "字符串方法比较多，可以通过`help(str)`或者`dir(str)`获取帮助。\n",
    "\n",
    "以下是一些常用的方法：\n",
    "\n",
    "* `split()` 基于**分隔符**将字符串分割成由若干子串组成的列表，如果不指定分割符，默认使用空白字符进行分割。\n",
    "* `join()` 与`split()`功能相反，将包含若干子串的列表分解，并将这些子串通过指定的**粘合用的字符串**合并成一个完整的大的字符串。\n",
    "* `find()` 查找返回字符串中第一次出现子串的位置（偏移量），失败时返回`-1`。\n",
    "* `index()` 与`find()`类似，但是查找失败时将触发`ValueError`异常。\n",
    "* `rfind()` 与`find()`类似，但返回最后一次子串出现的位置。\n",
    "* `startswith()` 判断字符串是否以特定前缀开头。\n",
    "* `endswith()` 判断字符串是否以特定后缀结尾。\n",
    "* `count()` 统计子串在字符串中出现的次数。\n",
    "* `is*` 判断字符串中字符是否符合某种类型或者规则。\n",
    "* `strip()` 返回移除开始和结尾空白字符的字符串，如果指定参数，将在字符串的开始和结尾移除参数中所包含的字符。\n",
    "* `upper()` `lower()` `swapcase()` 分别将字符串所有字母转换成大写、转换成小写、大小写转换。\n",
    "* `title()` 将字符串中所有单词的开头字母变成大写。\n",
    "* `capitalize()` 将字符串首字母变成大写。\n",
    "* `center()` `ljust()` `rjust()` 分别将字符串根据指定长度居中对齐、左对齐、右对齐。\n",
    "* `replace()` 进行简单的子串替换，需要传入的参数：需要被替换的子串，用于替换的新子串，以及需要替换多少处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "s = \"Hello, world!\"\n",
    "s.split()  # ['Hello,', 'world!']\n",
    "' '.join(s.split())  # s.split()\n",
    "s.find('world')  # 7\n",
    "#s.index('~')  # will cause ValueError\n",
    "s.is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.hycell": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "s.rfind('o')  # 8\n",
    "s.startswith('m')  # False\n",
    "s.endswith('!')  # True\n",
    "\n",
    "s.isalnum()  # False\n",
    "s.isdigit()  # False\n",
    "s.islower()  # False\n",
    "s.istitle()  # False\n",
    "s.isalpha()  # False\n",
    "\n",
    "s.strip('!d')  # 'Hello, worl'\n",
    "\n",
    "s.upper()     # 'HELLO, WORLD!'\n",
    "s.lower()     # 'hello, world!'\n",
    "s.swapcase()  # 'hELLO, WORLD!'\n",
    "\n",
    "s.title()     # 'Hello, World!'\n",
    "s.capitalize() # 'Hello, world!'\n",
    "\n",
    "s.center(20)  # '   Hello, world!    '\n",
    "s.ljust(20)   # 'Hello, world!       '\n",
    "s.rjust(20)   # '       Hello, world!'\n",
    "\n",
    "s.replace('o', 'O', 1)  # 'HellO, world!'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "练习："
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "name": "3_strings.ipynb",
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "273.783px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": false,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
